'use strict'
let nunjucks=require("nunjucks")

function createEnv(path,opts){
    path=path || 'views';
    opts= opts || {}

    let optsObj={
        autoescape: opts.autoescape || true,
        throwOnUndefined: opts.throwOnUndefined || false,
        trimBlocks: opts.trimBlocks || false,
        lstripBlocks: opts.lstripBlocks || false,
        watch: opts.watch || false,
        noCache: opts.watch || true
    }

    let a =nunjucks.configure(path,optsObj)
    return a;
}

module.exports=async(ctx,next)=>{
    let env=createEnv();

    ctx.render=function(views,modules){

        ctx.body=env.render(views,modules)
    }

    await next()
}